$(document).ready(function() {
    function bindTable() {
        $.ajax({
            url: "adminScreen.do?action=LargeScreenAction&event_submit_do_query_alarm=method",
            data: {
                deptId: deptIdAll,
                buildId: buildIdAll
            },
            type: "post",
            success: function(obj) {
                var str = '<table cellpadding="0" cellspacing="0">';
                str += '<tr>';
                str += '<th>报警对象</th>';
                str += '<th>分项名称</th>';
                str += '<th>能耗类型</th>';
                str += '<th>周期</th>';
                str += '<th>报警值</th>';
                str += '<th>实际能耗值</th>';
                str += '<td>超出值</td>';
                str += '</tr>';

                for (var i = 0; i < obj.length; i++) {
                    var item = obj[i];

                    str += '<tr class="trs" data-aoid="' + item.aoId + '" data-eiid="' + item.eId + '"' + '>';
                    str += '<td class="e9e9e9">' + item.alarmObject + '</td>';
                    str += '<td class="b0bb5c">' + item.eiName + '</td>';

                    if (item.ecCode == 'electric')
                        str += '<td class="f61041">电能</td>';
                    else if (item.ecCode == 'water')
                        str += '<td class="f61041">水能</td>';

                    if (item.timeType == '1')
                        str += '<td class="b0bb5c">日</td>';
                    else if (item.timeType == '2')
                        str += '<td class="b0bb5c">月</td>';
                    else if (item.timeType == '3')
                        str += '<td class="b0bb5c">年</td>';

                    str += '<td class="f61041">' + item.warnValue + '</td>';
                    str += '<td class="b0bb5c">' + item.actualValue + '</td>';
                    str += '<td class="f61041">' + item.beyond + '</td>';
                    str += '</tr>';
                }

                str += '</table>';

                $("#table").html(str);

                $(".trs").mouseenter(function() {

                    $(this).css("background-color", "#413932");
                    $(this).css("cursor", "pointer");
                });


                $(".trs").mouseout(function() {

                    $(this).css("background-color", "");
                });


                //$("#table").css("height", document.body.scrollHeight - 50);

            },
            error: function(jqXHR, textStatus, errorThrown) {

            }
        });

    }

    var diag = new Dialog();

    function showDialog(buildId, eiId) {
        var winWidth = 1000;
        var winHeight = 500;

        diag.Title = '专家诊断';
        diag.Width = winWidth;
        diag.Height = winHeight;
        diag.URL = 'warn.htm?buildId=' + buildId + '&eiId=' + eiId;
        diag.MessageTitle = "";
        diag.Message = "";
        diag.Drag = true; //取消拖拽
        diag.Modal=false;
        diag.show();
    }
    $('#table').on('click', 'table tr:gt(0)', function() {
        var buildId = $(this).data('aoid');
        var eiId = $(this).data('eiid');
        showDialog(buildId, eiId);
    });

    //动态请求图表数据
    function InitChart_BuildEmHourPie_e(strDate, buildId, eiIds) {
        jQuery.ajax({
            url: "adminScreen.do?action=ItemBuildHourAction&event_submit_do_query_data_pie=method",
            data: {
                date: strDate,
                buildId: buildId,
                eiIds: eiIds
            },
            type: "post",
            dataType: "json",
            success: function(obj) {
                if (obj.xAxisData.length > 0)
                    chartData = obj; // 查询到的数据赋值 给变量
                //动态改变tab名称
                BindChartPie_BuildEmHourPie_e(obj);
            },
            error: function(jqXHR, textStatus, errorThrown) {

            }
        });
    }
    //封装图表数据格式

    //封装饼图表数据格式
    function BindChartPie_BuildEmHourPie_e(obj) {
        myChart.clear();

        var option = {
            title: {
                text: '',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data: []
            },
            calculable: true,
            series: [{
                name: '用电量',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: obj.xydata
            }]
        };
        // 为echarts对象加载数据 
        myChart.setOption(option);
    }


    var timeType;
    var type;
    var eiIds;
    var strDate;
    var currentDate;
    var year;
    var month;
    var day;
    var strMonth;
    var strDay;
    var myChart;
    var myChart2;
    var chartData;
    var bar = echarts.init(document.getElementById('bar'), e_macarons);

    function draw_BuildEmHourPie_e() {
        buildId = buildId; //建筑id
        timeType = 'day'; //时间粒度（day,month,year）
        type = 'pie'; //图表类型
        eiIds = buildEle; //分项id集合（逗号分隔）
        strDate = ''; // 当前查询日期
        currentDate = new Date();
        year = currentDate.getFullYear(); //年
        month = currentDate.getMonth() + 1; //月
        day = currentDate.getDate() //日
        strMonth = '';
        strDay = '';
        if (month < 10) {
            strMonth = '0' + month;
        } else {
            strMonth = '' + month;
        }
        if (day < 10) {
            strDay = '0' + day;
        } else {
            strDay = '' + day;
        }
        if (timeType == 'day') {
            strDate = year + "-" + strMonth + "-" + strDay;
        } else if (timeType == 'month') {
            strDate = year + "-" + strMonth
        } else if (timeType == 'year') {
            strDate = year + '';
        }
        //创始化图表
        myChart = echarts.init(document.getElementById('BuildEmHourPie_e'), e_macarons);
        InitChart_BuildEmHourPie_e(strDate, buildId, eiIds);

    }

    //动态请求图表数据
    function InitChart_BuildEmHourPie_w(strDate, buildId, eiIds) {
        jQuery.ajax({
            url: "adminScreen.do?action=ItemBuildHourAction&event_submit_do_query_data_pie=method",
            data: {
                date: strDate,
                buildId: buildId,
                eiIds: eiIds
            },
            type: "post",
            dataType: "json",
            success: function(obj) {
                if (obj.xAxisData.length > 0)
                    chartData = obj; // 查询到的数据赋值 给变量
                //动态改变tab名称
                BindChartPie_BuildEmHourPie_w(obj);
            },
            error: function(jqXHR, textStatus, errorThrown) {

            }
        });
    }
    //封装饼图表数据格式
    function BindChartPie_BuildEmHourPie_w(obj) {
        myChart2.clear();

        var option = {
            title: {
                text: '',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data: []
            },
            calculable: true,
            series: [{
                name: '用水量',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: obj.xydata
            }]
        };
        // 为echarts对象加载数据 
        myChart2.setOption(option);
    }

    function draw_BuildEmHourPie_w() {

        timeType = 'day'; //时间粒度（day,month,year）
        type = 'pie'; //图表类型
        eiIds = buildWater;
        strDate = ''; // 当前查询日期
        currentDate = new Date();
        year = currentDate.getFullYear(); //年
        month = currentDate.getMonth() + 1; //月
        day = currentDate.getDate() //日
        strMonth = '';
        strDay = '';
        if (month < 10) {
            strMonth = '0' + month;
        } else {
            strMonth = '' + month;
        }
        if (day < 10) {
            strDay = '0' + day;
        } else {
            strDay = '' + day;
        }
        if (timeType == 'day') {
            strDate = year + "-" + strMonth + "-" + strDay;
        } else if (timeType == 'month') {
            strDate = year + "-" + strMonth
        } else if (timeType == 'year') {
            strDate = year + '';
        }
        //创始化图表
        myChart2 = echarts.init(document.getElementById('BuildEmHourPie_w'), e_macarons);
        InitChart_BuildEmHourPie_w(strDate, buildId, eiIds);
    }

    function InitBarChart(key1, key2, key3) {

        $.ajax({
            url: "adminScreen.do?action=CollectDeptAllRangeAction&event_submit_do_bar=method",
            data: {
                key1: key1,
                key2: key2,
                key3: key3
            },
            type: "post",
            success: function(obj) {

                BindBarChart(obj);

            },
            error: function(jqXHR, textStatus, errorThrown) {

            }
        });

    }

    function BindBarChart(obj) {
        bar.clear();
        bar.dispose();
        bar = echarts.init(document.getElementById('bar'), 'dark');

        var option = {
            tooltip: {
                trigger: 'axis',
                textStyle: {
                    align: 'left'
                }
            },
            toolbox: {
                show: true
            },
            calculable: true,
            grid: {
                x: 45,
                y: 25,
                x2: 20,
                y2: 25,
            },
            xAxis: [{
                type: 'category',
                data: obj.labels,
                axisLabel: {
                    textStyle: {
                        color: 'white',
                    }
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#505050',
                        width: 1
                    }
                },
                axisLine: { // 轴线
                    show: true,
                    lineStyle: {
                        color: '#505050'
                    }
                },
            }],
            yAxis: [{
                type: 'value',
                name: '单位：吨',
                splitNumber: 10,
                axisLabel: {
                    textStyle: {
                        color: 'white',
                    }
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#505050',
                        width: 1
                    }
                },
                axisLine: { // 轴线
                    show: true,
                    lineStyle: {
                        color: '#505050'
                    }
                },
            }],
            series: [{
                name: '标准煤',
                type: 'bar',
                data: obj.datas,
                itemStyle: {
                    normal: {
                        color: function(params) {
                            var colorList = [
                                '#E87C25'
                            ];
                            return colorList[0]
                        },
                        label: {
                            show: true,
                            position: 'top',
                            formatter: '{c}'
                        }
                    }
                }
            }]
        };

        // 为echarts对象加载数据 
        bar.setOption(option);
    }

    function bindBar() {
        var deptID = deptIds;
        var currentDate = new Date();
        var currentDateStr = currentDate.format('yyyy-MM-dd');
        InitBarChart(deptID, currentDateStr, currentDateStr);

        $("#bar").css("height", document.body.scrollHeight - $("#BuildEmHourPie_e").height() * 2 - 120);
    }

    bindTable();
    draw_BuildEmHourPie_e();
    draw_BuildEmHourPie_w();
    bindBar();

    setInterval(function() {
        bindTable();
        draw_BuildEmHourPie_e();
        draw_BuildEmHourPie_w();
        bindBar();
    }, 1000 * 60 * time);

})